package spotGUI.optionsGUI;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import managers.FastProperties;
import managers.OptionsManager;
import net.tomahawk.XFileDialog;
import java.util.ResourceBundle;


public class FileTransferPanel extends JPanel {
	
	private FastProperties options;
	private JLabel lblAutoAccept;
	private Box boxFileTransfersDir;
	private JTextField textFieldSearch1;
	private JButton btnSearch1;
	
	
	private XFileDialog fileChooser;
	private JButton btnEmptyCache;
	private Box horizontalBox;
	private JButton btnSearch2;
	private JTextField textFieldSearch2;
	private JLabel lblCartellaDiCaching;
	private Component horizontalStrut1;
	private Component horizontalStrut2;
	private JButton btnDefault;
	
	public FileTransferPanel() {
		
		options = OptionsManager.getSharedInstance().getOptionGroup("fileTransfer");
		fileChooser = new XFileDialog("Sfoglia");

		ItemListener checkBoxListener= new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == 1) {
					options.setProperty(((JCheckBox)e.getItem()).getName(), "true");
				} else {
					options.setProperty(((JCheckBox)e.getItem()).getName(), "false");
				}
				options.store();
			}
		};
		
		
		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);
		
		lblAutoAccept = new JLabel(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("FileTransferPanel.lblAutoAccept.text")); //$NON-NLS-1$ //$NON-NLS-2$
		springLayout.putConstraint(SpringLayout.NORTH, lblAutoAccept, 10, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, lblAutoAccept, 10, SpringLayout.WEST, this);
		add(lblAutoAccept);
		
		boxFileTransfersDir = Box.createHorizontalBox();
		springLayout.putConstraint(SpringLayout.NORTH, boxFileTransfersDir, 6, SpringLayout.SOUTH, lblAutoAccept);
		springLayout.putConstraint(SpringLayout.WEST, boxFileTransfersDir, 0, SpringLayout.WEST, lblAutoAccept);
		springLayout.putConstraint(SpringLayout.EAST, boxFileTransfersDir, -57, SpringLayout.EAST, this);
		add(boxFileTransfersDir);
		
		btnSearch1 = new JButton(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("FileTransferPanel.btnSearch1.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnSearch1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String folder = fileChooser.getFolder();
				if (folder != null) {
					options.setProperty("filesFolder", folder);
					options.store();
					textFieldSearch1.setText(folder);
				}
			}
		});
		boxFileTransfersDir.add(btnSearch1);
		
		horizontalStrut1 = Box.createHorizontalStrut(10);
		boxFileTransfersDir.add(horizontalStrut1);
		
		textFieldSearch1 = new JTextField();
		textFieldSearch1.setEditable(false);
		textFieldSearch1.setBorder(null);
		textFieldSearch1.setText(options.getProperty("filesFolder"));
		textFieldSearch1.setOpaque(false);
		textFieldSearch1.setName("filesFolder");
		boxFileTransfersDir.add(textFieldSearch1);
		
		btnEmptyCache = new JButton(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("FileTransferPanel.btnEmptyCache.text")); //$NON-NLS-1$ //$NON-NLS-2$
		springLayout.putConstraint(SpringLayout.WEST, btnEmptyCache, 0, SpringLayout.WEST, lblAutoAccept);
		btnEmptyCache.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				File f = new File (options.getProperty("cacheFolder"));
				File[] files = f.listFiles();
				for (int i = 0; i < files.length; i++) {
					files[i].delete();
				}
			}
		});
		add(btnEmptyCache);
		
		horizontalBox = Box.createHorizontalBox();
		springLayout.putConstraint(SpringLayout.NORTH, btnEmptyCache, 6, SpringLayout.SOUTH, horizontalBox);
		springLayout.putConstraint(SpringLayout.WEST, horizontalBox, 10, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, horizontalBox, 0, SpringLayout.EAST, boxFileTransfersDir);
		add(horizontalBox);
		
		btnSearch2 = new JButton(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("FileTransferPanel.btnSearch2.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnSearch2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String folder = fileChooser.getFolder();
				if (folder != null) {
					options.setProperty("cacheFolder", folder);
					options.store();
					textFieldSearch2.setText(folder);
				}
			}
		});
		horizontalBox.add(btnSearch2);
		
		horizontalStrut2 = Box.createHorizontalStrut(10);
		horizontalBox.add(horizontalStrut2);
		
		textFieldSearch2 = new JTextField();
		textFieldSearch2.setBorder(null);
		textFieldSearch2.setEditable(false);
		textFieldSearch2.setText(options.getProperty("cacheFolder"));
		textFieldSearch2.setOpaque(false);
		textFieldSearch2.setName("cacheFolder");
		horizontalBox.add(textFieldSearch2);
		
		lblCartellaDiCaching = new JLabel(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("FileTransferPanel.lblCartellaDiCaching.text")); //$NON-NLS-1$ //$NON-NLS-2$
		springLayout.putConstraint(SpringLayout.NORTH, horizontalBox, 6, SpringLayout.SOUTH, lblCartellaDiCaching);
		springLayout.putConstraint(SpringLayout.NORTH, lblCartellaDiCaching, 18, SpringLayout.SOUTH, boxFileTransfersDir);
		springLayout.putConstraint(SpringLayout.WEST, lblCartellaDiCaching, 0, SpringLayout.WEST, lblAutoAccept);
		add(lblCartellaDiCaching);
		
		btnDefault = new JButton(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("FileTransferPanel.btnDefault.text")); //$NON-NLS-1$ //$NON-NLS-2$
		springLayout.putConstraint(SpringLayout.NORTH, btnDefault, 0, SpringLayout.NORTH, btnEmptyCache);
		springLayout.putConstraint(SpringLayout.WEST, btnDefault, 6, SpringLayout.EAST, btnEmptyCache);
		btnDefault.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String folder = options.getProperty("cacheFolder_def");
				options.setProperty("cacheFolder", folder);
				options.store();
				textFieldSearch2.setText(folder);
			}
		});
		add(btnDefault);

	}
}
